package com.deyuanyun.pic.ae.service.impl;

import com.deyuanyun.pic.ae.controller.dto.SewerQuery;
import com.deyuanyun.pic.ae.domain.Sewer;
import com.deyuanyun.pic.ae.domain.SewerExample;
import com.deyuanyun.pic.ae.enumeration.DataStatedEnum;
import com.deyuanyun.pic.ae.enumeration.IsNewDateEnum;
import com.deyuanyun.pic.ae.enumeration.PositionRelationEnum;
import com.deyuanyun.pic.ae.mapper.SewerMapper;
import com.deyuanyun.pic.ae.mapper.dao.SewerDao;
import com.deyuanyun.pic.ae.service.AEBaseService;
import com.deyuanyun.pic.ae.service.SewerService;
import com.deyuanyun.pic.common.util.CommonUtil;
import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.common.util.web.Asserts;
import com.deyuanyun.pic.pipe.domain.Identification;
import com.deyuanyun.pic.pipe.domain.PipeLineGis;
import com.deyuanyun.pic.pipe.domain.PipeLineGisExample;
import com.deyuanyun.pic.pipe.mapper.PipeLineGisMapper;
import com.deyuanyun.pic.pipe.util.PipeMgrUtil;
import com.deyuanyun.pic.settings.service.pipe.PipeLineService;
import com.deyuanyun.pic.settings.support.biz.AttachmentUtil;
import com.deyuanyun.pic.settings.support.biz.PipeLineUtil;
import com.github.pagehelper.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * “地下排水排污设施” 服务接口 实现类
 *
 * @author huanglw
 * @date  2016-11-9
 *
 * History: [修改人][时间][修改描述]
 */
@Service
public class SewerServiceImpl extends AEBaseService implements SewerService {

    @Resource
    private SewerMapper sewerMapper;
    @Resource
    private SewerDao sewerDao;

    @Override
    public Page<Sewer> query(SewerQuery sewerQuery) {
        return sewerDao.query(sewerQuery);
    }

    @Override
    public Sewer find(String id) {
        return sewerMapper.selectByPrimaryKey(id);
    }

    @Override
    public int edit(Sewer sewer, List<String> imgs) {

        if (PositionRelationEnum.CROSS.toString().equals(sewer.getRelativePositionRelation())) {
            Identification identifications = PipeMgrUtil.queryIdentificationFlag(sewer.getCrossNagmInput());
            Asserts.assertLogic((identifications==null), "桩号:“"+sewer.getCrossNagmInput()+"”不存在，请重新输入");
            sewer.setCrossNagmInput(identifications.getId());
            sewer.setCrossNagmNumberInput(identifications.getSignNumber());
            // 计算起始绝对里程
            PositionObj crossPositionObj = getPositionInfo(sewer.getPipeLineId(), sewer.getCrossX(), sewer.getCrossY());
            if (crossPositionObj != null) {
                sewer.setCrossMileage(crossPositionObj.getMileage());
                sewer.setCrossNagm(crossPositionObj.getNagm());
                sewer.setCrossNagmNumber(crossPositionObj.getNagmNumber());
                sewer.setCrossNagmMileage(crossPositionObj.getNagmMileage());
            }
        } else {
            Identification beginIdentification = PipeMgrUtil.queryIdentificationFlag(sewer.getBeginNagmInput());
            Asserts.assertLogic((beginIdentification==null), "起点附近测试桩:“"+sewer.getBeginNagmInput()+"”不存在，请重新输入");
            sewer.setBeginNagmInput(beginIdentification.getId());
            sewer.setBeginNagmNumberInput(beginIdentification.getSignNumber());
            Identification endIdentification = PipeMgrUtil.queryIdentificationFlag(sewer.getEndNagmInput());
            Asserts.assertLogic((endIdentification==null), "终点附近测试桩:“"+sewer.getBeginNagmInput()+"”不存在，请重新输入");
            sewer.setEndNagmInput(endIdentification.getId());
            sewer.setEndNagmNumberInput(endIdentification.getSignNumber());

            // 计算起始绝对里程
            PositionObj beginPositionObj = getPositionInfo(sewer.getPipeLineId(), sewer.getBeginX(), sewer.getBeginY());
            if (beginPositionObj != null) {
                sewer.setBeginMileage(beginPositionObj.getMileage());
                sewer.setBeginNagm(beginPositionObj.getNagm());
                sewer.setBeginNagmNumber(beginPositionObj.getNagmNumber());
                sewer.setBeginNagmMileage(beginPositionObj.getNagmMileage());
            }
            // 计算终点绝对里程
            PositionObj endPositionObj = getPositionInfo(sewer.getPipeLineId(), sewer.getEndX(), sewer.getEndY());
            if (endPositionObj != null) {
                sewer.setEndMileage(endPositionObj.getMileage());
                sewer.setEndNagm(endPositionObj.getNagm());
                sewer.setEndNagmNumber(endPositionObj.getNagmNumber());
                sewer.setEndNagmMileage(endPositionObj.getNagmMileage());
            }
        }

        // 添加 图片信息
        String bizzId = StringUtils.isNotNullEmpty(sewer.getId()) ? sewer.getId() : CommonUtil.getUUID();
        for (String id : imgs) {
            AttachmentUtil.updateBusinessId(bizzId, id);
        }

        if (StringUtils.isNullEmpty(sewer.getId())){
            SewerExample sewerExample = new SewerExample();
            sewerExample.createCriteria().andDeletedEqualTo(Short.valueOf("0")).andNameEqualTo(sewer.getName());
            int countNum = sewerMapper.countByExample(sewerExample);
            Asserts.assertLogic(countNum > 0, "名称为:“"+sewer.getName()+"”地下排水排污设施 已经存在，不能重复插入");

            sewer.setId(bizzId);
            sewer.setCreateDate(new Date());
            sewer.setCreateUser(realName);
            sewer.setDataStated(DataStatedEnum.ALREADY_RELEASED.getCode());
            sewer.setIsNewest(Short.valueOf(IsNewDateEnum.YES.getCode()));
            return sewerMapper.insertSelective(sewer);
        }

        Sewer sewerObj = this.find(sewer.getId());
        Asserts.assertLogic(sewerObj == null, "名称为:“"+sewer.getName()+"”地下排水排污设施 不存在，不允许修改");
        Asserts.assertLogic(isAuditReleased(sewerObj.getDataStated()), "名称为:“"+sewer.getName()+"”地下排水排污设施 已审核通过,不允许编辑");
        if (isAuditDismissed(sewerObj.getDataStated())){
            sewer.setDataStated(DataStatedEnum.ALREADY_RELEASED.getCode());
        }
        return sewerMapper.updateByPrimaryKeySelective(sewer);
    }

    @Override
    public int audit(Sewer sewer) {
        Sewer sewerObj = this.find(sewer.getId());
        Asserts.assertLogic(sewerObj == null, "名称为:“"+sewer.getName()+"”地下排水排污设施 不存在，不允许修改");
        Asserts.assertLogic(isAudit(sewerObj.getDataStated()), "名称为:“"+sewer.getName()+"”地下排水排污设施 已审核,不允许重复审核");
        return sewerMapper.updateByPrimaryKeySelective(sewer);
    }

    @Override
    public int delete(String id) {
        Sewer sewer = this.find(id);
        Asserts.assertLogic(sewer == null, "名称为:“"+sewer.getName()+"”地下排水排污设施 不存在，不允许删除");
        Asserts.assertLogic(isAuditReleased(sewer.getDataStated()), "名称为:“"+sewer.getName()+"”地下排水排污设施 已审核通过,不允许删除");
        sewer = new Sewer();
        sewer.setId(id);
        sewer.setDeleted(Short.valueOf("1"));
        sewer.setUpdateDate(new Date());
        sewer.setUpdateUser(realName);
        sewer.setUpdateRemark("删除");
        return sewerMapper.updateByPrimaryKeySelective(sewer);
    }

}
